var sliderStep = 0;
var delaySlider = 7500;
var maxStepSlider = 1;
var timeAnime = 700;
var timer;

var slideTopImage = function(){
    sliderStep++;
    if( sliderStep >= maxStepSlider ) sliderStep = 0;
    gostepSlider( sliderStep );
};
var gostepSlider = function( step ){
	sliderStep = step;
	clearInterval(timer);
	timer = setInterval('slideTopImage()', 3500);
	
	var hauteur = $('#slider-top-image').height();
	var marginTop = hauteur*sliderStep;
        
	$( '#step-0' ).animate({marginTop: -(marginTop)},timeAnime,'easeInOutBack');
};

$(document).ready(function(){
    /* slider vertical top image */
    if( typeof $('#slider-top-image') != 'undefined' && $('#slider-top-image').length > 0 ){
        maxStepSlider = $('#slider-top-image li').length;
        timer = setInterval('slideTopImage()', delaySlider);
    }
    
    /* scroll popup */
    $(document).scroll( function(){
        $('.popup').css( 'top', $(document).scrollTop() + 'px' );
    });
    
    /* close popup */
    $('.popup .close').click(function(e){
        closePopup();
    });
    
    /* galerie*/
    if(typeof $('#galerie') !== 'undefined' && $('#galerie').length > 0){
        Galleria.loadTheme(baseUrl+themePath+'/galleria/galleria.classic.min.js');
        Galleria.configure({
            lightbox: true,
            thumbnails: 'numbers',
            autoplay: true,
            showInfo: false
        });
        Galleria.run('#galerie');
    }

    /* form contact */
    if(typeof $('#form_contact') !== 'undefined' && $('#form_contact').length > 0){
        var validatorForm = $('.form_user').validate({
            rules:{
                nom:'required'
                ,email:{
                    required:true,
                    email:true
                }
            }
            ,messages:{
                email:{
                    required:'Votre email est obligatoire'
                    ,email:'Indiquez une adresse email valide'
                }
                ,nom:'Votre nom est obligatoire'
            }
            ,submitHandler: function(form){
                form.submit();
            }
            ,invalidHandler: function(){
                $('div.validatError').remove();
            }
            ,focusInvalid: false
            ,errorElement: "div"
            ,errorClass: "validatError"
            ,errorPlacement: function(error, element) {
                if($('div#error'+element.attr('name'))){
                    $('div#error'+element.attr('name')).remove();
                }
                $('body').append(error);
                var offset = element.offset();
                error.attr('id','error'+element.attr('name'));
                error.css('left', offset.left + element.outerWidth());
                error.css('top', offset.top);
                error.click(function(){
                    $(this).fadeOut('fast');
                    $(this).remove();
                });
            }
        });
        $("input").focus(function() {
            $('div#error' + $(this).attr("name")).remove();
        });
    }
});

/* gestion des popups */
var openPopup = function(idPopup){
    $('.popup').hide();
    if( typeof $('#'+idPopup) !== 'undefined' && $('#'+idPopup).length>0 ){
        $('#'+idPopup).fadeIn('fast');
    }
};
var closePopup = function(){
    $('.popup').fadeOut('fast');
};

/* loader graphique */
var showAjaxLoader = function(){
    $('#ajax_loader').show();
};
var hideAjaxLoader = function(){
    $('#ajax_loader').hide();
};

var doAjax = function(target,data,callback,errorMsg,errorContainer){
    showAjaxLoader();
    $.ajax({
            type: "POST",
            url: target,
            data: data,
            dataType: 'json'
    })
    .always(function(){
        hideAjaxLoader();
    })
    .done(function(retour){
        if(retour.success){
            if(callback === 'reload') location.reload();
            else callback(retour.datas);
        }else{
            if(errorContainer!=='')$(errorContainer).html(errorMsg);
            else alert(errorMsg);
        }
    })
    .fail(function(jqXHR, textStatus){
        alert('Erreur de requête : ' + textStatus);
    });
};